//
//  LoginViewController.m
//  DoubanDemo1
//
//  Created by lanou3g on 15/11/21.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginView.h"
#import "RegisterViewController.h"
//#import "DataUser.h"
#import "FileHandle.h"
#import "DataBase.h"
#import "UserInformation.h"
@interface LoginViewController ()

@property (nonatomic,strong)LoginView *loginView;

@end

@implementation LoginViewController

- (void)loadView
{
    self.loginView = [[LoginView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.loginView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"用户登陆";
    
    //左侧返回按钮
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftItemAction:)];
    self.navigationItem.leftBarButtonItem = leftItem;
    // Do any additional setup after loading the view.
    [self.loginView.loginBtn addTarget:self action:@selector(loginViewAction:) forControlEvents:UIControlEventTouchUpInside];
    //右下注册按钮
    [self.loginView.registerBtn addTarget:self action:@selector(registerBtnAction:) forControlEvents:UIControlEventTouchUpInside];
}

//左侧返回按钮方法
- (void)leftItemAction:(UIBarButtonItem *)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

//右下注册按钮
- (void)registerBtnAction:(UIButton *)sender
{
    RegisterViewController *registerVC = [[RegisterViewController alloc]init];
 
    registerVC.title = @"注册";
    [self.navigationController pushViewController:registerVC animated:YES];
    
}

- (void)loginViewAction:(UIButton *)sender
{
    NSString *userStr = self.loginView.passWordLTView.textField.text;
    NSString *passwordStr = self.loginView.userNameLTView.textField.text;
    UserInformation *user = [[DataBase sharedDataBase] selectInformationWithUserName:userStr];
    BOOL result = [passwordStr isEqualToString:user.password];
    if (result == YES) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"登陆成功" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [alertView show];
        NSTimer *time = [NSTimer timerWithTimeInterval:1 target:alertView selector:@selector(dismissWithClickedButtonIndex:animated:) userInfo:nil repeats:NO];
        [alertView dismissWithClickedButtonIndex:1 animated:YES];
        //添加事件
        [FileHandle shareInstance].username = user.uesrName;
        [FileHandle shareInstance].password = user.password;
        [FileHandle shareInstance].arrayMovie = user.movieAarry;
        [FileHandle shareInstance].arrayActivity = user.Activity;
        [[FileHandle shareInstance] setloginState:YES];
        [[FileHandle shareInstance] synchronize];
        
        [self.navigationController dismissViewControllerAnimated:YES completion:^{
        }];
        
        
    }else {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"失败" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [alertView show];
        NSTimer *time = [NSTimer timerWithTimeInterval:1 target:alertView selector:@selector(dismissWithClickedButtonIndex:animated:) userInfo:nil repeats:NO];
        
        [time fire];
        [alertView dismissWithClickedButtonIndex:1 animated:YES];
        
    }

    
    
    
    
    
    
    
    
    
  
                    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
